<template>
    <div class="container-content">
        <el-form
            ref="elForm"
            :model="formData"
            :rules="rules"
            size="medium"
            label-width="100px"
        >
            <!-- <el-form-item label="编码" prop="code">
                <el-input
                    v-model="formData.code"
                    placeholder="请输入编码"
                    clearable
                    :style="{ width: '100%' }"
                ></el-input>
            </el-form-item> -->
            <el-form-item label="名称" prop="name">
                <el-input
                    v-model="formData.name"
                    placeholder="请输入名称"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="创建人" prop="userId">
                <el-input
                    v-model="formData.userId"
                    placeholder="请输入创建人"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="采购组织" prop="purchaseDept">
                <el-input
                    v-model="formData.purchaseDept"
                    placeholder="请输入采购组织"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="价目表对象" prop="priceObject">
                <el-select
                    v-model="formData.priceObject"
                    placeholder="请选择"
                    :style="{ width: '100%' }"
                >
                    <el-option
                        v-for="item in priceObjectOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="币别" prop="currencyId">
                <el-input
                    v-model="formData.currencyId"
                    placeholder="请输入币别"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item>

            <el-form-item label="供应商" prop="supplierId">
                <el-input
                    v-model="formData.supplierId"
                    placeholder="请输入供应商"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="定价员" prop="pricingOfficer">
                <el-input
                    v-model="formData.pricingOfficer"
                    placeholder="请输入定价员"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="价格类型" prop="priceType">
                <el-input
                    v-model="formData.priceType"
                    placeholder="请输入价格类型"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="价外税" prop="extraTax">
                <el-radio v-model="formData.extraTax" :label="true" border
                    >是</el-radio
                >
                <el-radio v-model="formData.extraTax" :label="false" border
                    >否</el-radio
                >
            </el-form-item>
            <el-form-item prop="type" label="类型">
                <el-radio v-model="formData.type" :label="0" border
                    >价目表</el-radio
                >
                <el-radio v-model="formData.type" :label="1" border
                    >折扣表</el-radio
                >
            </el-form-item>
            <el-form-item prop="includingTax" label="是否含税">
                <el-radio v-model="formData.includingTax" :label="true" border
                    >是</el-radio
                >
                <el-radio v-model="formData.includingTax" :label="false" border
                    >否</el-radio
                >
            </el-form-item>
            <el-form-item prop="isDefault" label="是否默认">
                <el-radio v-model="formData.isDefault" :label="true" border
                    >是</el-radio
                >
                <el-radio v-model="formData.isDefault" :label="false" border
                    >否</el-radio
                >
            </el-form-item>
            <el-form-item prop="isTemp" label="是否暂存">
                <el-radio v-model="formData.isTemp" :label="true" border
                    >是</el-radio
                >
                <el-radio v-model="formData.isTemp" :label="false" border
                    >否</el-radio
                >
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input
                    v-model="formData.remark"
                    type="textarea"
                    placeholder="请输入备注"
                    :autosize="{ minRows: 4, maxRows: 4 }"
                    :style="{ width: '100%' }"
                ></el-input>
            </el-form-item>

            <div class="desc-title">业务明细</div>
            <el-table
                :data="formData.detailList"
                style="width: 100%"
                highlight-current-row
                border
            >
                <template #empty>
                    <el-button plain type="primary" round @click="addNewRow"
                        >添加新行
                    </el-button>
                </template>
                <el-table-column label="操作" width="120">
                    <template slot-scope="scope">
                        <el-button
                            plain
                            icon="el-icon-plus"
                            size="mini"
                            @click="handleDataAdd(scope.$index, scope.row)"
                        ></el-button>
                        <el-button
                            plain
                            icon="el-icon-minus"
                            size="mini"
                            @click="handleDataDelete(scope.$index, scope.row)"
                        ></el-button>
                    </template>
                </el-table-column>
                <el-table-column label="物料编码">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="materialId"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input v-model="scope.row.materialId"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="物料名称">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="materialName"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.materialName"
                                disabled
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="计价单位">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="pricingUnit"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.pricingUnit"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="从">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="fromWhere"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input v-model="scope.row.fromWhere"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="至">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="toWhere"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input v-model="scope.row.toWhere"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="单价">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="unitPrice"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input v-model="scope.row.unitPrice"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="含税单价">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="includingTaxPrice"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.includingTaxPrice"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="税率%">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="taxRate"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input v-model="scope.row.taxRate"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="价格系数">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="priceCoefficient"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.priceCoefficient"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="价格下限">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="priceFloor"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input v-model="scope.row.priceFloor"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="价格上限">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="priceCeiling"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.priceCeiling"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="生效日期" width="160">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="effectiveDate"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-date-picker
                                v-model="scope.row.effectiveDate"
                                type="date"
                                placeholder="选择日期"
                                style="width: 100%"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="失效日期" width="160">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="expirationDate"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-date-picker
                                v-model="scope.row.expirationDate"
                                type="date"
                                placeholder="选择日期"
                                style="width: 100%"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="行禁用">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="lineDisabled"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-checkbox
                                v-model="scope.row.lineDisabled"
                            ></el-checkbox>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="需求组织">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="requireDept"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.requireDept"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="自定义基础资料1">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="customBasicDataOne"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.customBasicDataOne"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="自定义基础资料2">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="customBasicDataTwo"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.customBasicDataTwo"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="自定义辅助资料1">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="customSupportDataOne"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.customSupportDataOne"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="自定义辅助资料2">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="customSupportDataTwo"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.customSupportDataTwo"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="自定义文本1">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="customTextOne"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.customTextOne"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="自定义文本2">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="customTextTwo"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.customTextTwo"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="自定义价格1">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="customPriceOne"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.customPriceOne"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="自定义价格2">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="customPriceTwo"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input
                                v-model="scope.row.customPriceTwo"
                            ></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="备注">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="remark"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-input v-model="scope.row.remark"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="行审核状态">
                    <template slot-scope="scope">
                        <el-form-item
                            prop="reviewStatus"
                            label-width="0px"
                            style="margin: 0"
                        >
                            <el-radio
                                v-model="scope.row.reviewStatus"
                                :label="0"
                                >未审核</el-radio
                            >
                            <el-radio
                                v-model="scope.row.reviewStatus"
                                :label="1"
                                >已审核</el-radio
                            >
                        </el-form-item>
                    </template>
                </el-table-column>
            </el-table>
        </el-form>
    </div>
</template>
<script>
import { add } from "@/api/base/data";
import { editKingPurchasePriceList,updataKingPurchasePriceList } from "@/api/bim/outher";

export default {
    components: {},
    props: ['id'],
    data() {
        return {
            flag:false,
            formData: {
                detailList: [],
                code: undefined,
                currencyId: "",
                extraTax: undefined,
                includingTax: undefined,
                isDefault: undefined,
                isTemp: undefined,
                name: "",
                priceObject: "",
                priceType: "",
                pricingOfficer: "",
                purchaseDept: "",
                remark: "",
                supplierId: "",
                type: 0,
                userId: undefined,
            },
            priceObjectOptions: [
                {
                    label: "选项一",
                    value: "1",
                },
                {
                    label: "选项二",
                    value: "2",
                },
            ],
            rules: {
                name: [
                    {
                        required: true,
                        message: "请输入名称",
                        trigger: "blur",
                    },
                ],
                priceType: [
                    {
                        required: true,
                        message: "请输入价格类型",
                        trigger: "blur",
                    },
                ],

                purchaseDept: [
                    {
                        required: true,
                        message: "请输入采购组织",
                        trigger: "blur",
                    },
                ],
                currencyId: [
                    {
                        required: true,
                        message: "请输入币别",
                        trigger: "blur",
                    },
                ],
                // pricingUnit: [
                //     {
                //         required: true,
                //         message: "请输入计价单位",
                //         trigger: "blur",
                //     },
                // ],
                // unitPrice: [
                //     {
                //         required: true,
                //         message: "请输入单价",
                //         trigger: "blur",
                //     },
                // ],
                // includingTaxPrice: [
                //     {
                //         required: true,
                //         message: "请输入含税单价",
                //         trigger: "blur",
                //     },
                // ],
                // effectiveDate: [
                //     {
                //         required: true,
                //         message: "请输入生效日期",
                //         trigger: "blur",
                //     },
                // ],
                // expirationDate: [
                //     {
                //         required: true,
                //         message: "请输入失效日期",
                //         trigger: "blur",
                //     },
                // ],
            },
        };
    },
    computed: {},
    watch: {},
    created() {},
    mounted() {
        console.log(this.id);
        
       if (this.id!='') {
        updataKingPurchasePriceList(this.id).then((res)=>{
            this.formData=res.data
            this.flag=true
        })
       }
    },
    methods: {
        submitForm(cb) {
           
               if (this.flag) {
                 this.$refs["elForm"].validate((valid) => {
                if (!valid) return;
                // TODO 提交表单
                editKingPurchasePriceList(this.formData, "KingPurchasePrice").then((res) => {
                    if (res.code === 200) {
                        cb();
                        this.$message.success("修改成功");
                        this.flag=false
                    }
                });
            });
               }else{
                 this.$refs["elForm"].validate((valid) => {
                if (!valid) return;
                // TODO 提交表单
                add(this.formData, "KingPurchasePrice").then((res) => {
                    if (res.code === 200) {
                        cb();
                        this.$message.success("新增成功");
                    }
                });
            });
               }
            
        },
        addNewRow() {
            this.handleDataAdd();
        },
        handleDataAdd() {
            if (this.formData.detailList == undefined) {
                this.formData.detailList = [];
            }
            let obj = {};
            obj.allocationMethod = "";
            obj.calMethod = "";
            obj.customBasicDataOne = "";
            obj.customBasicDataTwo = "";
            obj.customPriceOne = "";
            obj.customPriceTwo = "";
            obj.customSupportDataOne = "";
            obj.customSupportDataTwo = "";
            obj.customTextOne = "";
            obj.customTextTwo = "";
            obj.discountAmount = "";
            obj.discountBasis = "";
            obj.discountAmount = "";
            obj.discountDirection = "";
            obj.discountItems = "";
            obj.discountRate = "";
            obj.discountType = "";
            obj.effectiveDate = "";
            obj.expirationDate = "";
            obj.fromWhere = "";
            obj.toWhere = "";
            obj.includingTaxPrice = "";
            obj.lineDisabled = "";
            obj.materialId = "";
            obj.priceCeiling = "";
            obj.priceCoefficient = "";
            obj.priceFloor = "";
            obj.priceInfoId = "";
            obj.pricingUnit = "";
            obj.remark = "";
            obj.requireDept = "";
            obj.reviewStatus = "";
            obj.taxRate = "";
            obj.unitPrice = "";
            obj.userId = "";
            obj.effectiveDate = "";
            obj.expirationDate = "";
            this.formData.detailList.push(obj);
        },
        handleDataDelete(index, row, type) {
            this.$confirm("请确认是否要删除此行？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.formData.detailList.splice(index, 1);
                    this.$message({
                        type: "success",
                        message: "删除成功!",
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        resetForm() {
            this.$refs["elForm"].resetFields();
        },
    },
};
</script>
<style></style>
